#include <dos.h>
#include <stdio.h>
#include <go32.h>
#include <sys/farptr.h>
#include <sys/nearptr.h>

#include <stdlib.h>
#include "window.h"

#include "utypes.h"
#include "video-text.h"
#include "video-graphics.h"

#include "global.h"

#include "sound.h"
#include "physics.h"
#include "input.h"
#include "bg.h"
#include "level.h"
#include "xpm.h"
#include "game.h"

/** \todo Mover o código que processa o protagonista para um ficheiro próprio */
int main(int argc, char *argv[])
{
	int mode = 1;

	if(argc != 2)
	{
		drawFrame("ALEX KIDD LCOM",high_blue,0,0,WIDTH,HEIGHT);
		
		printStringAt("      [.        [..                        [..   [..          [..     [..",3,2,high_blue);
		printStringAt("     [. ..      [..                        [..  [..    [.     [..     [..",3,3,high_blue);
		printStringAt("    [.  [..     [..   [..    [..   [..     [.. [..            [..     [..",3,4,high_blue);
		printStringAt("   [..   [..    [.. [.   [..   [. [..      [. [.      [.. [.. [.. [.. [..",3,5,high_blue);
		printStringAt("  [...... [..   [..[..... [..   [.         [..  [..   [..[.   [..[.   [..",3,6,high_blue);
		printStringAt(" [..       [..  [..[.         [.  [..      [..   [..  [..[.   [..[.   [..",3,7,high_blue);
		printStringAt("[..         [..[...  [....   [..   [..     [..     [..[.. [.. [.. [.. [..",3,8,high_blue);

		printStringAt("___                         _                   ",15,11,high_blue);
		printStringAt(" | __    |V| _  _| _    | ||_)   | | _  __ |  _|",15,12,high_blue);
		printStringAt("_|_| |   | |(_|(_|(/_   |_||     |^|(_) |  | (_|",15,13,high_blue);

		printStringAt("by ei06009 ei07041 & ei09137",50,15,high_blue);
		printStringAt("Como usar: alexk mode(1, 2, 3, 4)",3,17,high_blue);
		
		printStringAt("Escolha o modo grafico:",3,19,high_blue);
		printStringAt("1 - 640x480",3,20,high_blue);
		printStringAt("2 - 800x600",3,21,high_blue);
		printStringAt("3 - 1024x768",3,22,high_blue);
		printStringAt("4 - 1280x1024",3,23,high_blue);
		scanf("%d",&mode);

		system("cls");
	}
	else if(argc == 2)
	{
		mode = atoi(argv[1]);
	}	

	switch(mode)
	{
	case 1:		//640*480
		mode = R640x480;
		break;
	case 2:		//800*600
		mode = R800x600;
		break;
	case 3:		//1024*768
		mode = R1024x768;
		break;
	case 4:		//1280*1024
		mode = R1280x1024;
		break;
	default:
		printf("Modo nao suportado!");
		return 1;
	}
	
	// Sons e timer
	sound_init ();
	// Janelas e modo gráfico
	winsys_init (mode);
	// Imagens redimensionadas
	xpm_init ();
	// Entrada de dados
	input_init ();
	// Gestor de jogo
	game_init ();

	// Toca uma música
	Sound snd = sound_load ("sounds/stlalive.txt");
	sound_music_play (snd);
	
	do {
		// Lê os dados do teclado
		input_keyboard_read ();
		// Faz o update das janelas
		winsys_update();
		// desenha as janelas 
		winsys_draw ();
		// processa colisões
		level_collision ();
		// Espera 20 milisegundos e toca música ao mesmo tempo
		sound_milisleep(20);
			
	} while(!GAME_QUIT);
	
	// Termina tudo
	xpm_finish();
	winsys_finish ();
	input_finish ();
	sound_finish();

	return 0;
}
